#!/bin/bash
# 编译脚本，用于设置环境变量并执行ODrive固件编译

# 设置工具链路径
toolchain_path="D:/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin"
make_path="C:/Program Files (x86)/GnuWin32/bin"

# 添加到PATH
export PATH="$toolchain_path:$make_path:$PATH"

# 验证工具是否可用
echo "检查编译环境..."
which make
if [ $? -ne 0 ]; then
    echo "错误: 未找到make工具，请确保已安装Make for Windows并添加到路径"
    echo "建议从 http://gnuwin32.sourceforge.net/packages/make.htm 下载安装"
    exit 1
fi

which arm-none-eabi-gcc
if [ $? -ne 0 ]; then
    echo "错误: 未找到ARM工具链，请检查路径是否正确: $toolchain_path"
    exit 1
fi

echo "编译环境检查通过!"
echo "make版本: $(make --version | head -n1)"
echo "ARM工具链版本: $(arm-none-eabi-gcc --version | head -n1)"

# 清理之前的构建
echo -e "\n清理之前的构建..."
make clean

# 创建构建目录
if [ ! -d "build" ]; then
    mkdir build
fi

# 执行编译
echo -e "\n开始编译ODrive固件..."
make -j4

# 检查编译结果
if [ $? -eq 0 ]; then
    echo -e "\n编译成功!"
    echo "输出文件位于build目录:"
    ls -la build/*.elf build/*.hex build/*.bin 2>/dev/null || echo "未找到输出文件，请检查编译日志"
else
    echo -e "\n编译失败，请查看上面的错误信息"
    exit 1
fi